package dto

import (
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
)

type IresToken struct {
	AccessToken  string `json:"accessToken" `
	RefreshToken string `json:"refreshToken" `
}

type RoleName struct {
	Role string `json:"perm" uri:"perm" binding:"required,max=10" required_msg:"角色名 perm 最大使用10个字符" validate:"required"`
}

type SysList struct {
	Method   string `json:"method" form:"method" binding:"required"  required_msg:"method 为必填项目[]string"`
	Url      string `json:"url" form:"url" binding:"required"  required_msg:"url 为必填项目[]string"`
	Fn       string `json:"fn_name" form:"fn_name" binding:"required"  required_msg:"fn_name 为必填项目[]string"`
	Fullpath string `json:"fullpath" form:"fullpath" binding:"required"  required_msg:"fullpath 为必填项目[]string"`
}

type SysListAc struct {
	SysList []SysList `json:"syslist" form:"syslist" binding:"required"  required_msg:"syslist 为必填项目[]syslist"`
}

func (m *SysList) FilterSysList(routesInfo *gin.RoutesInfo) *[]SysList {
	var apiList []SysList
	var url string
	for _, route := range *routesInfo {
		cacheList := strings.Split(route.Path, viper.GetString("service.prefix"))
		if len(cacheList) > 1 {
			url = cacheList[1]
		} else {
			url = cacheList[0]
		}
		apiList = append(apiList, SysList{Method: route.Method, Fullpath: url + "_" + route.Method, Url: url, Fn: route.Handler})
	}
	return &apiList
}

type IresUriUsername struct {
	Username string `json:"username" uri:"username" binding:"required,max=10" required_msg:"用户名 username 最大使用10个字符" validate:"required"`
}

type ChangeRolesByName struct {
	Username string `uri:"username" json:"username"  binding:"required,max=10"  required_msg:"用户名 username 为必填项目" validate:"required"`
	// RoleList []RoleObj `json:"rolelist" form:"rolelist"  binding:"required"  required_msg:"角色名 rolelist 为必填项目" validate:"required,dive"`
	//BUG uri与params,同时数据是个切片时无法校验
	// RoleList []string `form:"rolelist" json:"rolelist" binding:"required" required_msg:"角色列表 rolelist 为必填项目" validate:"required,dive"`
	RoleList []string `form:"rolelist" json:"rolelist"`
}
type RoleObj struct {
	Role string `json:"role" form:"role" binding:"required,max=10" required_msg:"角色名 role 最大使用10个字符" validate:"required"`
}
